package com.ebelter.seneo.ui.fragments.addinfos;


import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.Selection;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.LinkMovementMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.activity_manager.ActivityManagers;
import com.ebelter.seneo.R;
import com.ebelter.seneo.ui.activitys.SeneoAddInfoActivity;
import com.ebelter.seneo.ui.activitys.SeneoLoginActivity;
import com.ebelter.seneo.ui.activitys.WebActivity;
import com.ebelter.seneo.ui.fragments.BaseFragment;
import com.ebelter.seneo.utils.SeneoUtils;


//填写密码
public class AddInfo_Name_Fragment extends BaseFragment{



    public static final String TAG = "AddInfo_Name_Fragment";

    EditText lg_nick_ed;
    EditText lg_email_ed;
    EditText lg_psw_ed;
    EditText rg_psw2_ed;
    ImageView lg_psw_iv;
    TextView lg_register_tv;

    TextView rg_tip_tv;
    Button register_bt;

    private boolean displayPsw;

    LinearLayout addname_name_ll;
    LinearLayout addname_email_ll;
    LinearLayout lg_psw_ll;
    LinearLayout rg_psw2_ll;



    @Override
    protected int setLayoutResouceId() {
        return R.layout.activity_seneo_register;
    }

    @Override
    protected void initView() {
        FV();
    }

    private void FV() {
        lg_nick_ed=findViewById(R.id.lg_nick_ed);
        lg_email_ed=findViewById(R.id.lg_email_ed);
        lg_psw_ed=findViewById(R.id.lg_psw_ed);
        rg_psw2_ed=findViewById(R.id.rg_psw2_ed);
        lg_psw_iv=findViewById(R.id.lg_psw_iv);
        lg_register_tv=findViewById(R.id.lg_register_tv);
        register_bt=findViewById(R.id.register_bt);
        rg_tip_tv=findViewById(R.id.rg_tip_tv);

        addname_name_ll=findViewById(R.id.addname_name_ll);
        addname_email_ll=findViewById(R.id.addname_email_ll);
        lg_psw_ll=findViewById(R.id.lg_psw_ll);
        rg_psw2_ll=findViewById(R.id.rg_psw2_ll);


        ViewUtils.setEdInputFilters(lg_nick_ed);
        ViewUtils.setEdInputFilters2(lg_psw_ed);
        ViewUtils.setEdInputFilters2(rg_psw2_ed);
        //ViewUtils.setEdInputFilters(lg_email_ed,50);


        addname_name_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SeneoUtils.showSoftInputFromWindow(getActivity(),lg_nick_ed);
            }
        });

        addname_email_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SeneoUtils.showSoftInputFromWindow(getActivity(),lg_email_ed);
            }
        });

        lg_psw_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SeneoUtils.showSoftInputFromWindow(getActivity(),lg_psw_ed);
            }
        });

        rg_psw2_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SeneoUtils.showSoftInputFromWindow(getActivity(),rg_psw2_ed);
            }
        });


        rg_psw2_ed.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)){
                    rg_psw2_ed.setHint("Re-type Password");
                }else {
                    rg_psw2_ed.setHint("Password");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

                if (TextUtils.isEmpty(s)){
                    rg_psw2_ed.setHint("Re-type Password");
                }else {
                    rg_psw2_ed.setHint("Password");
                }
            }
        });

    }

    @Override
    protected void initData(Bundle arguments) {

       // String tip = "By signing up,you agree to the Terms of service,and Privacy Policy";
        String tip = "By signing up,you agree to the <a href=\"123\">Terms of service</a>,and <a href=\"456\">Privacy Policy</a>";

        rg_tip_tv.setText(getClickableHtml(tip));

        rg_tip_tv.setMovementMethod(LinkMovementMethod.getInstance());

    }


    private void setLinkClickable(final SpannableStringBuilder clickableHtmlBuilder,
                                  final URLSpan urlSpan) {
        int start = clickableHtmlBuilder.getSpanStart(urlSpan);
        int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
        int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);
        ClickableSpan clickableSpan = new ClickableSpan() {

            public void onClick(View view) {
                String url = urlSpan.getURL();
                if (TextUtils.equals("123",url)){
                    WebActivity.openWebActivity(getActivity(),"file:///android_asset/Terms_of_Service.htm","Terms of service");
                }else if (TextUtils.equals("456",url)){
                    WebActivity.openWebActivity(getActivity(),"file:///android_asset/Privacy_Policy.htm","Privacy Policy");
                }
                //ToastUtil.show(url);
            }

        };
        clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);
    }

    private CharSequence getClickableHtml(String html) {
        Spanned spannedHtml = Html.fromHtml(html);
        SpannableStringBuilder clickableHtmlBuilder = new SpannableStringBuilder(spannedHtml);
        URLSpan[] urls = clickableHtmlBuilder.getSpans(0, spannedHtml.length(), URLSpan.class);
        for (final URLSpan span : urls) {
            setLinkClickable(clickableHtmlBuilder, span);
        }
        return clickableHtmlBuilder;
    }

    @Override
    protected void setListener() {
        lg_psw_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pswdisChange();
            }
        });
        lg_register_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        register_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=lg_nick_ed.getText().toString();
                String email=lg_email_ed.getText().toString();
                String psw1=lg_psw_ed.getText().toString();
                String psw2=rg_psw2_ed.getText().toString();

                if ((getActivity()) != null) {
                    ((SeneoAddInfoActivity)getActivity()).startRegisterHostUser(name,email,psw1,psw2);
                }
            }
        });
    }






    public void pswdisChange() {
        displayPsw=!displayPsw;
        updatePswModel();
    }

    private void updatePswModel(){
        if (displayPsw){
            //密码框变为可见
            lg_psw_ed.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            rg_psw2_ed.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            ViewUtils.setImageResource(lg_psw_iv,R.drawable.ic_psw2);
            Selection.setSelection(lg_psw_ed.getText(), lg_psw_ed.getText().length());
            Selection.setSelection(rg_psw2_ed.getText(), rg_psw2_ed.getText().length());
        }else {
            //密码不可见
            lg_psw_ed.setTransformationMethod(PasswordTransformationMethod.getInstance());
            rg_psw2_ed.setTransformationMethod(PasswordTransformationMethod.getInstance());
            ViewUtils.setImageResource(lg_psw_iv,R.drawable.ic_psw1);
            Selection.setSelection(lg_psw_ed.getText(), lg_psw_ed.getText().length());
            Selection.setSelection(rg_psw2_ed.getText(), rg_psw2_ed.getText().length());
        }
    }



    public void login() {
        ActivityManagers.getInstance().closeActivity(SeneoLoginActivity.TAG);
        startActivity(new Intent(getActivity(),SeneoLoginActivity.class));
        ActivityManagers.getInstance().closeActivity(SeneoAddInfoActivity.TAG);
    }


}
